package org.vdab.projects.tasks.shared.entities.impl;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.vdab.projects.tasks.client.util.ICommand;
import org.vdab.projects.tasks.shared.Utils;
import org.vdab.projects.tasks.shared.entities.IBaseEntity;

import com.google.gwt.user.client.rpc.IsSerializable;

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class BaseEntity<T extends BaseEntity<T> > implements IBaseEntity<T>, IsSerializable {
	


	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	protected String key;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
    protected String simpleKey;
	
	protected BaseEntity(){		
	}
	
	public String getKey() {		
		return simpleKey;
	}
	
//	@SuppressWarnings("unchecked")
//	public T getSso(){
//		return (T)this;
//	}
	
	@SuppressWarnings("unchecked")
	public T init(){
		this.simpleKey = Utils.generateRandomKey();
		return (T)this;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void execute(ICommand<T> command) throws Exception {		
		command.execute((T)this);
	}
}
